﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenWaves.EPiServer.Localization.Model;

namespace OpenWaves.EPiServer.Localization.Transformations
{
    public partial class TranslationKeysTemplate
    {
        private static readonly string[] DefaultFiles = new[]
            {
                @"OnlineCenter_", "tinyMCE", "workflowFoundation_", @"newtext", @"languageDA", @"languageDE", @"languageEN", @"languageES", @"languageFR",
                @"languageJA", @"languageNL", @"languageNO", @"languagePT", @"languageSV", @"languageZH"
            };

        private readonly TranslationModel model;

        public TranslationKeysTemplate(IEnumerable<string> filePaths)
            : this(filePaths, true)
        {
        }

        public TranslationKeysTemplate(IEnumerable<string> filePaths, bool excludeDefaultFiles)
        {
            if (excludeDefaultFiles)
            {
                filePaths = filePaths.Where(p => IsDefaultFile(p) == false);
            }

            this.model = TranslationModel.FromFiles(filePaths);
        }

        public TranslationKeysTemplate(TranslationModel model)
        {
            this.model = model;
        }

        public int EPiServerVersion
        {
            get { return this.model.EPiServerVersion; }
            set { this.model.EPiServerVersion = value; }
        }

        public TranslationModel Model
        {
            get { return this.model; }
        }

        internal static bool IsDefaultFile(string path)
        {
            var fileName = Path.GetFileName(path);

            if (string.IsNullOrEmpty(fileName))
            {
                return false;
            }
            return DefaultFiles.Any(f => fileName.StartsWith(f, StringComparison.InvariantCultureIgnoreCase));
        }
    }
}